/**
 * 系统监控控制器
 */

package com.ojoin.trade.t0loan_core.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.ojoin.trade.common.Constants;
import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.ControllerException;
import com.ojoin.trade.common.utils.CheckCodeGenerateUtil;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.SMSHelper;
import com.ojoin.trade.common.utils.UUIDUtils;
import com.ojoin.trade.common.utils.Version;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.Result.Status;
import com.ojoin.trade.common.web.domain.SessionUser;
import com.ojoin.trade.common_core.utils.TradeUtils;

@Controller
@RequestMapping("/system")
public class SystemMonitorController  extends SimpleBaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(SystemMonitorController.class);

	/**
	 * 前端JS错误上报记录
	 * @param info
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/error", method = RequestMethod.GET)
	@ResponseStatus(value = HttpStatus.OK)
	public void onError(HttpServletRequest request,String info) throws UnsupportedEncodingException {
        String pageRefer=request.getHeader("Referer");
		LogUtils.error(logger,URLDecoder.decode(URLDecoder.decode(info, Constants.ENCODING),
				Constants.ENCODING)+"||"+pageRefer);
	}

	/**
	 * version
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/version", method = RequestMethod.GET)
	public Result version(HttpServletRequest request, String mobile) {
		return new Result(Status.OK, Version.CURRENT_CLASSPATH);
	}

	/**
	 * 账户重要信息变动验证
	 * @param request
	 * @param mobile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyAccountInfo", method = RequestMethod.GET)
	public Result getModifyAccountInfoCheckCode(HttpServletRequest request) {
		String mhtNo=TradeUtils.getMhtNo(request);
		String mobile=TradeUtils.getMerchUser(request).getMobile();
		
		//生成code
		String validateCode = CheckCodeGenerateUtil.genRandomString();
		//缓存code
		SMSHelper.setModifyAccountInfoValidateCode(mhtNo,validateCode);
		//发送code
        SMSHelper.sendModifyAccountInfoCode(mhtNo,mobile,validateCode);
        
        String maskmobile = CommonUtils.maskMobile(mobile);
        logger.info("商户账户变动{} 验证手机号{}",new String[]{mhtNo,maskmobile});
        
		return new Result(Status.OK, String.format("已将短信发送到尾号为%s的手机号", maskmobile));
	}

	 
	@ResponseBody
	@RequestMapping(value = "/modifyAccountInfo", method = RequestMethod.POST)
	public String VerifyModifyAccountInfoCheckCode(HttpServletRequest request, String mobileVerifyCode) {
		SessionUser user=CommonUtils.getSessionUser(request);
		String mhtNo=user.getMhtNo();
		
		boolean isValidShortMsgCode=SMSHelper.validateModifyAccountInfoCode(mhtNo, mobileVerifyCode);

		logger.info("商户账户变动{}  验证结果{}",new String[]{mhtNo,String.valueOf(isValidShortMsgCode)});
		
		return String.valueOf(isValidShortMsgCode);
	}
	
	
	/**
	 * 绑定银行卡验证
	 * @param request
	 * @param mobile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addBankCardCheckCode", method = RequestMethod.GET)
	public Result getAddBankCardCheckCode(HttpServletRequest request, String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return new Result(Status.ERROR, "手机号空.");
		}
		
		String mhtNo=TradeUtils.getMhtNo(request);
		
		//生成code
		String validateCode = CheckCodeGenerateUtil.genRandomString();
		//缓存code
		SMSHelper.setAddBankCardValidateCode(mhtNo,validateCode);
		//发送code
		SMSHelper.sendAddBankCardCode(mhtNo,mobile,validateCode);
		
		String maskmobile = CommonUtils.maskMobile(mobile);
        logger.info("商户绑卡{} 验证手机号{}",new String[]{mhtNo,mobile});
        return new Result(Status.OK, String.format("已将短信发送到尾号为%s的手机号", maskmobile));
	}

	 
	@ResponseBody
	@RequestMapping(value = "/addBankCardCheckCode", method = RequestMethod.POST)
	public String VerifyAddBankCardCheckCode(HttpServletRequest request, String mobileVerifyCode) {
		SessionUser user=CommonUtils.getSessionUser(request);
		String mhtNo=user.getMhtNo();
		
		boolean isValidShortMsgCode=SMSHelper.validateAddBankCardCode(mhtNo, mobileVerifyCode);
		
		logger.info("商户绑卡{}  验证结果{}",new String[]{mhtNo,String.valueOf(isValidShortMsgCode)});
		
		return String.valueOf(isValidShortMsgCode);
	}
	
	/**
	 * 实名认证验证
	 * @param request
	 * @param mobile
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/mobileCheckCode", method = RequestMethod.GET)
	public Result getMobileCheckCode(HttpServletRequest request, String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return new Result(Status.ERROR, "手机号空.");
		}
		
		String mhtNo=TradeUtils.getMhtNo(request);
		
		//生成code
		String validateCode = CheckCodeGenerateUtil.genRandomString();
		//缓存code
		SMSHelper.setMobileValidateCode(mhtNo,validateCode);
		//发送code
        SMSHelper.sendMobileCode(mhtNo,mobile,validateCode);
        
        String maskmobile = CommonUtils.maskMobile(mobile);
        logger.info("商户实名认证{} 验证手机号{}",new String[]{mhtNo,mobile});
        
        return new Result(Status.OK, String.format("已将短信发送到尾号为%s的手机号", maskmobile));
	}

	/**
	 * @param request
	 * @param mobileVerifyCode  企业
	 * @param mobileVerifyCodeP 个人
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/mobileCheckCode", method = RequestMethod.POST)
	public String VerifyMobileCheckCode(HttpServletRequest request, String mobileVerifyCode,String mobileVerifyCodeP) {
		SessionUser user=CommonUtils.getSessionUser(request);
		String mhtNo=user.getMhtNo();
		String mhtType=user.getMchType();
		
		String verifyCode=IConstDef.Type_INDIVIDUAL.equals(mhtType)?mobileVerifyCodeP:mobileVerifyCode;
		boolean isValidShortMsgCode=SMSHelper.validateMobileCode(mhtNo, verifyCode);
		
		logger.info("商户实名认证{}   验证结果{}",new String[]{mhtNo,String.valueOf(isValidShortMsgCode)});
		
		return String.valueOf(isValidShortMsgCode);
	}
	
	/**
	 * Token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/token", method = RequestMethod.GET)
	public Result token(HttpServletRequest request) {
		String token = UUIDUtils.create();
		HttpSession session=request.getSession(false);
		if(null==session){
			throw new ControllerException("长时间未操作，请重新登录");
		}
		session.setAttribute("token", token);
		return new Result(Status.OK, token);
	}

}
